package homework4.exp2;

public class TestAccount implements Runnable {

    private Account account=new Account();
    @Override
    public void run() {
        for (int i=0;i<5;i++){
            makeWithdrawal(100);
            if (account.getBalance()<0){
                System.out.println("账户透支！");
            }
        }
    }

    private synchronized void makeWithdrawal(int amt){
        if (account.getBalance()>=amt){
            System.out.println(Thread.currentThread().getName()+"准备取款");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {

            }
            account.withdraw(amt);
            System.out.println(Thread.currentThread().getName()+"完成取款");
        }else {
            System.out.println("余额不足支付"+Thread.currentThread().getName()+"的取款，余额为"+account.getBalance());
        }
    }
}
